#include "encoder.h"

/**
 * @brief 编码器初始化
 * @param  无
 * @retval  无
 **/
void Encoder_TIM2_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_ICInitTypeDef TIM_ICInitStruct;

    //开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    //初始化GPIO--PA0、PA1
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    //初始化定时器。
    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period = 65535;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);

    TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //配置编码器模式

    //初始化输入捕获
    TIM_ICStructInit(&TIM_ICInitStruct);
    TIM_ICInitStruct.TIM_ICFilter = 10;
    TIM_ICInit(TIM2, &TIM_ICInitStruct);

    //配置溢出更新中断标志位
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    //清零定时器计数值
    TIM_SetCounter(TIM2, 0);

    //开启定时器
    TIM_Cmd(TIM2, ENABLE);
}

/**
 * @brief 编码器初始化
 * @param  无
 * @retval  无
 **/
void Encoder_TIM3_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_ICInitTypeDef TIM_ICInitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period = 65535;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);

    TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

    TIM_ICStructInit(&TIM_ICInitStruct);
    TIM_ICInitStruct.TIM_ICFilter = 10;
    TIM_ICInit(TIM3, &TIM_ICInitStruct);

    TIM_ClearFlag(TIM3, TIM_FLAG_Update);
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

    TIM_SetCounter(TIM3, 0);

    TIM_Cmd(TIM3, ENABLE);
}
/**
 * @brief 编码器初始化
 * @param  None
 * @retval  None
 **/
void Encoder_Init(void)
{
    Encoder_TIM2_Init();
    Encoder_TIM3_Init();
}

/**
 * @brief 编码器读取
 * @param  A编码器数据存储地址 B编码器数据存储地址
 * @retval  None
 **/
void Read_Speed(int32_t *a, int32_t *b)
{
    *a = (short)TIM_GetCounter(TIM2);
    TIM_SetCounter(TIM2, 0);

    *b = (short)TIM_GetCounter(TIM3);
    TIM_SetCounter(TIM3, 0);
}

/**
 * @brief 定时器2中断服务函数
 * @param  无
 * @retval  无
 **/
void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != 0)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    }
}

/**
 * @brief 定时器3中断服务函数
 * @param  无
 * @retval  无
 **/
void TIM3_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != 0)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    }
}
